package com.huixin.powermonitor.controller;


import cn.hutool.db.AbstractDb;
import cn.smallbun.screw.core.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixin.powermonitor.common.QueryPageParam;
import com.huixin.powermonitor.common.Result;

import com.huixin.powermonitor.entity.Device;
import com.huixin.powermonitor.entity.PerformanceAnalysis;

import com.huixin.powermonitor.service.DeviceService;
import com.huixin.powermonitor.service.PerformanceAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huixin
 * @since 2025-07-08
 */
@RestController
@RequestMapping("/performance-analysis")
public class PerformanceAnalysisController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PerformanceAnalysisService performanceAnalysisService;


    @PostMapping("listPage")
    public Result alarm(@RequestBody QueryPageParam params)  {

        int pageNum = params.getPageNum();
        int pageSize = params.getPageSize();

        Page<PerformanceAnalysis> page = new Page<>(pageNum, pageSize);
        HashMap<String,Object> map = params.getParam();
        LambdaQueryWrapper<PerformanceAnalysis> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Device> deviceLambdaQueryWrapper= new LambdaQueryWrapper<>();
        if( map.containsKey("deviceName") && StringUtils.isNotBlank(map.get("deviceName").toString()))  {
            String deviceName = map.get("deviceName").toString();
            deviceLambdaQueryWrapper.like(Device::getDeviceName,deviceName);
            List<Device> deviceList=deviceService.list(deviceLambdaQueryWrapper);
            for(Device d:deviceList){
                queryWrapper.like(PerformanceAnalysis::getDeviceId,d.getDeviceId());
            }
        }
        IPage<PerformanceAnalysis> PerformanceAnalysisDataPage = performanceAnalysisService.page(page, queryWrapper);
        List<PerformanceAnalysis> records = PerformanceAnalysisDataPage.getRecords();
        for(PerformanceAnalysis record : records) {
            Device device = deviceService.getById(record.getDeviceId());
            if (device != null) {
                record.setDeviceName(device.getDeviceName());
            } else {
                record.setDeviceName("未知设备");
            }
        }
        return Result.suc(PerformanceAnalysisDataPage.getTotal(),records);
    }

    @PostMapping("add")
    public Result addPerformanceAnalysis(@RequestBody PerformanceAnalysis performanceAnalysis){
        performanceAnalysisService.save(performanceAnalysis);
        return Result.suc();
    }

    @PostMapping("delete")
    public Result deletePerformanceAnalysis(@RequestBody Long[] ids){
        performanceAnalysisService.removeByIds(Arrays.asList(ids));
        return Result.suc();
    }
}
